package com.knife.file.config;

import com.knife.cache.common.helper.CacheManager;
import com.knife.cache.redis.config.RedisCachePropertySourceFactory;
import com.knife.cache.redis.constants.RedisConstant;
import com.knife.cache.redis.entity.RedisCacheCategoryProperty;
import com.knife.cache.redis.entity.RedisCacheProperty;
import com.knife.cache.redis.helper.RedisCacheManager;
import com.knife.file.infra.cache.KnifeStorageConfigCache;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2021年11月22日 星期一 11:45 上午
 * @description
 * @since 1.0
 */
@Configuration("KnifeStoreConfigRedisCacheConfigure")
@PropertySource(value = {"classpath:redis-cache-file.yml"}, ignoreResourceNotFound = true, factory = RedisCachePropertySourceFactory.class)
@ConditionalOnResource(resources = {"classpath:redis-cache-file.yml"})
public class KnifeStoreConfigRedisCacheConfigure {

    @Bean(name = "cacheManager")
    @ConditionalOnMissingBean(value = {CacheManager.class})
    public CacheManager cacheManager() {
        return new RedisCacheManager();
    }

    @Bean(name = "knifeStorageRedisCacheCategoryProperty")
    public RedisCacheCategoryProperty redisCacheCategoryProperty() {
        return new RedisCacheCategoryProperty(RedisConstant.STORAGE);
    }

    @Bean(name = "knifeStorageConfigCacheProperty")
    @ConfigurationProperties(prefix = "knife.cache.redis.file")
    public RedisCacheProperty knifeStorageConfigCacheProperty() {
        return new RedisCacheProperty();
    }


    @Bean(name = "knifeStorageConfigCache")
    public KnifeStorageConfigCache knifeStorageConfigCache(@Qualifier(value = "knifeStorageConfigCacheProperty") RedisCacheProperty knifeStorageConfigCacheProperty,
                                                           @Qualifier(value = "knifeStorageRedisCacheCategoryProperty") RedisCacheCategoryProperty redisCacheCategoryProperty) {
        return new KnifeStorageConfigCache(redisCacheCategoryProperty, knifeStorageConfigCacheProperty);
    }

}
